package edu.mse.camel.client.posterboard.ui.actions;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.GroupRequest;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import edu.mse.camel.client.posterboard.ui.editpart.IPosterBoardDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.util.EditpartUtil;
import edu.mse.camel.client.whiteboard.gef.ui.editpart.IWhiteboardEditPart;

public class DeleteFromPosterboardAction extends SelectionAction {

		/**
	     * 
	     */
	    public final static String DELETE_FROM_DIAGRAM_ACTION_REQUEST =
	            "com.cea.papyrus.requests.deletefromdiagram";

	    /**
	     * 
	     */
	    public final static String DELETE_FROM_DIAGRAM_ACTION_ID =
	            "edu.mse.camel.client.posterboard.delete";
	    
	    /**
	     * Default constructor.
	     * 
	     * @param part
	     */
	    public DeleteFromPosterboardAction(IWorkbenchPart part) {
	        super(part);
	        setText("Delete From Diagram");
	        setId(DELETE_FROM_DIAGRAM_ACTION_ID);
	        setToolTipText("Delete element(s) from the diagram");
	        ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
	        setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
	        setHoverImageDescriptor(getImageDescriptor());

	    }

	    /*
	     * (non-Javadoc)
	     * 
	     * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
	     */
	    @Override
	    protected boolean calculateEnabled() {
	        return canPerformAction();
	    }

	    /**
	     * @return
	     */
	    private boolean canPerformAction() {
	        if (getSelectedObjects().isEmpty()) {
	            return false;
	        }
	        return true;
	    }

	    /**
	     * @return
	     */
	    @SuppressWarnings("unchecked")
		private Command getCommand() {
	        List editparts = getSelectedObjects();
	        CompoundCommand cc = new CompoundCommand();
	        for (int i = 0; i < editparts.size(); i++) {
	            EditPart part = (EditPart) editparts.get(i);
	            if (part instanceof IPosterBoardDiagramEditpart
	            	|| part instanceof IWhiteboardEditPart) {
	            	GroupRequest groupRequest = new GroupRequest(RequestConstants.REQ_DELETE);
	            	cc.add(part.getCommand(groupRequest));
	            }
	            if (EditpartUtil.isPapyrusEditpart(part)) {
	            	//UML element
	            	Request request = new Request(DELETE_FROM_DIAGRAM_ACTION_REQUEST); 
	            	cc.add(part.getCommand(request));
	            }
	        }
	        return cc;
	    }

	    /*
	     * (non-Javadoc)
	     * 
	     * @see org.eclipse.jface.action.Action#run()
	     */
	    @Override
	    public void run() {
	        // open dialog window
	        BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
	            public void run() {
	                execute(getCommand());
	            }
	        });
	    }

	}